<template>
  <ContentField>
    <div class="title">
      <h3>注册</h3>
    </div>
    <div class="box">
      <div class="inputbox">
        <el-form :model="form" ref="formRef" :rules="rules">
          <el-form-item prop="nickName">
            <el-input v-model="form.nickName" placeholder="请输入昵称"></el-input>
          </el-form-item>
          <el-form-item prop="username">
            <el-input v-model="form.username" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" show-password v-model="form.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input type="password" show-password v-model="form.confirmPassword" placeholder="请再次输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </ContentField>
</template>

<script setup>
import ContentField from "@/components/ContentField.vue";
import { ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();

import { ElMessage } from 'element-plus';

import { postJson } from '@/utils/api'

const formRef = ref(null);
const form = ref({
  nickName: '',
  username: '',
  password: '',
  confirmPassword: '',
});
const validateusernamel = async (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入账号'));
  } else if (await postJson('/checkUsername', { username: form.value.username }).then(res => {
    return res.status;
  }) == 201) {
    callback(new Error('该账号已被注册'));
  } else {
    callback();
  }
};

const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== form.value.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

const rules = ref({
  nickName: [
    { required: true, message: '请输入昵称', trigger: 'blur' }
  ],
  username: [
    { validator: validateusernamel, trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  confirmPassword: [
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
});

const submitForm = async () => {
  try {
    await formRef.value.validate(async (valid) => {
      if (valid) {
        await postJson('/register', form.value).then(res => {
          if (res.status == 200) {
            ElMessage.success("注册成功")
            router.push({ name: 'login' });
          }
        }).catch(error=>{
          console.log(error)
        })
      } else {
        console.log('表单验证失败');
      }
    });
  } catch (error) {
    console.log("error")
  }
}
</script>

<style scoped>
.title {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  margin-bottom: 10px;
}

.avatar-wrapper {
  width: 56px;
  height: 56px;
  border: 1px solid #ccc;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 10px;
  color: #666;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

button {
  width: 100%;
}

.error-message {
  display: flex;
  height: 10px;
  margin-top: 5px;
  justify-content: left;
  color: rgb(236, 68, 68);
}

div.box {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.inputbox {
  width: 60%;
}

input {
  width: 100%;
  height: 40px;
  padding: 5px 15px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 16px;
  color: #666;
  box-sizing: border-box;
}

input::placeholder {
  color: #999;
}
</style>